<template>
  <el-dialog
    title="添加员工"
    :visible="dialogShow"
    width="480px"
    :close-on-click-modal="false"
    @close="closeDialog"
    @open="checkAllRole"
  >
    <!-- 表单接口 -->
    <div class="form-container">
      <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-width="80px">
        <el-form-item label="员工姓名" prop="name">
          <el-input v-model="addForm.name" />
        </el-form-item>
        <el-form-item label="登录账号" prop="userName">
          <el-input v-model="addForm.userName" />
        </el-form-item>
        <el-form-item label="联系方式" prop="phonenumber">
          <el-input v-model="addForm.phonenumber" />
        </el-form-item>
        <el-form-item label="分配角色" prop="roleId">
          <el-select v-model="addForm.roleId" placeholder="请选择角色">
            <el-option
              v-for="item in roleList"
              :key="item.roleId"
              :label="item.roleName"
              :value="item.roleId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="员工状态" prop="status">
          <el-radio-group v-model="addForm.status">
            <el-radio :label="0">禁用</el-radio>
            <el-radio :label="1">启用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <el-button size="mini" @click="closeDialog">取 消</el-button>
      <el-button size="mini" type="primary" @click="confirm">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { addEmployeeAPI, checkAllRoleAPI, getEmployeeDetailAPI, editEmployeeInfoAPI } from '@/api/employee'
export default {
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogVisible: true, // 控制弹框打开关闭
      roleList: [], // 角色列表
      addForm: {
        name: '',
        phonenumber: '',
        roleId: '',
        status: 1,
        userName: ''
      },
      addFormRules: {
        name: [
          { required: true, message: '请输入员工姓名', trigger: 'blur' }
        ],
        userName: [
          { required: true, message: '请输入登录账号', trigger: 'blur' }
        ],
        phonenumber: [
          { required: true, message: '请输入联系方式', trigger: 'blur' }
        ],
        roleId: [
          { required: true, message: '请分配角色', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择员工状态', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    closeDialog() {
      this.$emit('closeDialog')
      this.$refs.addForm.resetFields()
      this.addForm = {
        name: '',
        phonenumber: '',
        roleId: '',
        status: 1,
        userName: ''
      }
    },
    confirm() {
      this.$refs.addForm.validate(async flag => {
        if (!flag) return
        if (this.addForm.id) {
          await editEmployeeInfoAPI(this.addForm)
          this.$message.success('编辑用户成功')
        } else {
          await addEmployeeAPI(this.addForm)
          this.$message.success('添加用户成功')
        }
        this.closeDialog()
      })
    },
    async checkAllRole() {
      const { data } = await checkAllRoleAPI()
      this.roleList = data
    },
    async getEmployeeDetail(iid) {
      const { data } = await getEmployeeDetailAPI(iid)
      const { name, phonenumber, roleId, status, userName, id } = data
      this.addForm = { name, phonenumber, roleId, status, userName, id }
    }
  }
}
</script>
<style lang="less" scoped>

</style>
